package cn.tedu.weibo.service.Impl;


import cn.tedu.weibo.mapper.UserMapper;
import cn.tedu.weibo.pojo.dto.UserLoginDTO;
import cn.tedu.weibo.pojo.dto.UserRegDTO;
import cn.tedu.weibo.pojo.entity.User;
import cn.tedu.weibo.pojo.vo.UserVO;
import cn.tedu.weibo.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public int register(UserRegDTO userRegDTO) {
        String username = userRegDTO.getUsername();
        UserVO userVO = userMapper.selectByUsername(username);
        if(userVO != null){
            return 0;
        }
        User user = new User();
        BeanUtils.copyProperties(userRegDTO, user);
        user.setCreated(new Date());
        int res = userMapper.insert(user);
        log.info("用户注册结果：{}", res);
        return res;
    }

    @Override
    public UserVO login(UserLoginDTO userLoginDTO) {
        String username = userLoginDTO.getUsername();
        String password = userLoginDTO.getPassword();
        UserVO userVO = userMapper.selectByUsername(username);
        if(userVO == null || !userVO.getPassword().equals(password)){
            return null;
        }
        return userVO;
    }
}
